package com.itSession.cn.controller.app;

import com.itSession.cn.entity.UserInfo;
import com.itSession.cn.mapper.UserInfoMapper;
import com.itSession.cn.util.Result;
import com.itSession.cn.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: UserInfoController
 * @description: UserInfoController
 * @author: hanP
 * @create: 2021-03-29 17:11:16
 */
@Api(tags = "用户模块")
@RequestMapping("/userInfo")
@RestController
public class UserInfoController {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("list")
    public Result listAll() {
    	try {
    		List<UserInfo> list = userInfoMapper.listAll();
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }

    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "className", value = "班级", required = false, paramType = "query" ,dataType="string"),
            @ApiImplicitParam(name = "userRole", value = "角色", required = false, paramType = "query" ,dataType="int"),
    })
    @RequestMapping("listByClassNameAndRole")
    public Result listByClassNameAndRole(@ApiIgnore UserInfo userInfo) {
        try {
            Map<String, Object> map = new HashMap<>();

            map.put("userRole", userInfo.getUserRole());
            List<UserInfo> list = userInfoMapper.listByClassNameAndRole(map);
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }


    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userRole", value = "角色", required = false, paramType = "query" ,dataType="int")
    })
    @RequestMapping("listByRole")
    public Result listByRole(Integer userRole) {
        try {
            List<UserInfo> list = userInfoMapper.listByRole(userRole);
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }

    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userAccount", value = "账号", required = false, paramType = "query" ,dataType="int"),
            @ApiImplicitParam(name = "userRole", value = "角色", required = false, paramType = "query" ,dataType="int"),
    })
    @RequestMapping("listByAccount")
    public Result listByAccount(@ApiIgnore UserInfo userInfo) {
        try {
            Map<String, Object> map = new HashMap<>();
            //map.put("userAccount", userInfo.getUserAccount());
            map.put("userRole", userInfo.getUserRole());
            List<UserInfo> list = userInfoMapper.listByAccount(map);
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }

    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("listByUserId")
    public Result listByUserId(String userId) {

        try {
            Map<String, Object> map = new HashMap<>();
            map.put("userId",userId);
            List<UserInfo> list = userInfoMapper.listByUserId(map);
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }

    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("listByUser")
    public Result listByUser(String userId) {
        List<UserInfo> list = new ArrayList<>();
        String[] split1 = userId.split(",");
        try {
            for (int i = 0; i < split1.length; i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("userId",split1[i]);
                List<UserInfo> list0 = userInfoMapper.listByUserId(map);
                list.addAll(list0);
            }

            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }

    /**
     * 分页查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("listByParam")
    public Result listByParam(String keyword) {
    	try {
         	Map<String, Object> map = new HashMap<>();
            map.put("keyword",keyword);
    		List<UserInfo> list = userInfoMapper.listByParam(map);
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }



    /**
     * 根据主键查询
     *
     * @param userId 主键
     * @return 返回记录，没有返回null
     */
    @ApiOperation(value = "根据主键查询", httpMethod = "POST", notes = "加载数据", response = UserInfo.class)
     @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "编号", required = false, paramType = "query" ,dataType="int")
    })
    @RequestMapping("getById")
    public Result getById(@ApiIgnore Integer userId) {
    	try {
    	 	UserInfo userInfo = userInfoMapper.getById(userId);
         	return ResultGenerator.genSuccessResult(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500, e.getMessage());
        }
       
    }
    
    
    /**
     * 新增，忽略null字段
     *
     * @param userInfo 新增的记录
     * @return 返回影响行数
     */
      @ApiOperation(value = "新增，忽略null字段", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @ApiImplicitParams({
                @ApiImplicitParam(name = "userId", value = "编号", required = false, paramType = "query" ,dataType="int"),
                @ApiImplicitParam(name = "userAccount", value = "账号", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "userName", value = "姓名", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "userRole", value = "角色 1.管理员 2.普通用户 3.家政人员", required = false, paramType = "query" ,dataType="string"),
        })
    @RequestMapping("insertIgnoreNull")
    public Result insertIgnoreNull(@ApiIgnore UserInfo userInfo) {
    	try {
    		userInfoMapper.insertIgnoreNull(userInfo);
            return ResultGenerator.genSuccessResult();
       } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }
    
    
    /**
     * 修改，忽略null字段
     *
     * @param userInfo 修改的记录
     * @return 返回影响行数
     */
    @ApiOperation(value = "修改，忽略null字段", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @ApiImplicitParams({
                @ApiImplicitParam(name = "userId", value = "编号", required = false, paramType = "query" ,dataType="int"),
                @ApiImplicitParam(name = "userAccount", value = "账号", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "userName", value = "姓名", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "userRole", value = "角色 1.管理员 2.普通用户 3.家政人员", required = false, paramType = "query" ,dataType="string"),
        })
    @RequestMapping("updateIgnoreNull")
    public Result updateIgnoreNull(@ApiIgnore UserInfo userInfo) {
    	 try {
         	 userInfoMapper.updateIgnoreNull(userInfo);
            return ResultGenerator.genSuccessResult();
         } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }
    
    
     /**
     * 删除记录
     *
     * @param userInfo 待删除的记录
     */
    @ApiOperation(value = "删除记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("delete")
    public Result delete(@ApiIgnore UserInfo userInfo) {
    	 try {
         	 userInfoMapper.delete(userInfo);
             return ResultGenerator.genSuccessResult();
         } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }
    
   
    
}